﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKCodeBase.WinForm;
using ModelChecking.ModelValidation.FireCompartment;

namespace ModelChecking.ModelValidation
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ModelessFormApp : IExternalApplication
	{
		public Result OnShutdown(UIControlledApplication application)
		{
			if (ModelessFormApp.m_dlg != null && !ModelessFormApp.m_dlg.IsDisposed)
			{
				ModelessFormApp.m_dlg.Dispose();
				ModelessFormApp.m_dlg = null;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result OnStartup(UIControlledApplication application)
		{
			ModelessFormApp.m_dlg = null;
			ModelessFormApp.m_appThis = this;
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public void ShowForm(ExternalCommandData cmdData, List<ViewPlan> lstViewPlans)
		{
			if (ModelessFormApp.m_dlg == null || ModelessFormApp.m_dlg.IsDisposed)
			{
				ModelessFromRequestHandler modelessFromRequestHandler = new ModelessFromRequestHandler();
				ExternalEvent exEvent = ExternalEvent.Create(modelessFromRequestHandler);
				ModelessFormApp.m_dlg = new DlgFireOutlineWinDoorWallCheck(cmdData, modelessFromRequestHandler, exEvent, lstViewPlans);
				Process currentProcess = Process.GetCurrentProcess();
                WindowHandle windowHandle = new WindowHandle(currentProcess.MainWindowHandle);
				ModelessFormApp.m_dlg.m_wndHandleRevit = windowHandle;
				ModelessFormApp.m_dlg.Show(windowHandle);
			}
		}

		internal static ModelessFormApp m_appThis;

		internal static DlgFireOutlineWinDoorWallCheck m_dlg;
	}
}
